package com.heima.wemedia.service.impl;

import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.service.WmMaterialService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    //图片上传
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {
            //校验参数是否合法
        if (multipartFile.isEmpty() || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

            //把文件上传到minio中得到文件的访问地址  预防文件重名问题 需要用UUID修改原始文件名
            //文件原始名
        String filename = multipartFile.getOriginalFilename();
        //UUID修改原名
        filename = UUID.randomUUID().toString().replaceAll("-","")+filename
                .substring(filename.lastIndexOf("."));
        //通过封装的MinIo上传方法读取minio地址
        String path = fileStorageService.uploadImgFile("", filename, multipartFile.getInputStream());

        /*
                String fileId = null;
        try {
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }
        * */

        //从ThreadLocal中获取当前登录的用户信息
        WmUser user = WmThreadLocalUtil.getUser();
        //将对应信息保存到数据库中
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(path);
        wmMaterial.setUserId(user.getId());
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);

        //添加数据
        save(wmMaterial);
        //返回结果
        return ResponseResult.okResult(wmMaterial);
    }

    //查询所有 素材列表展示
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //校验参数是否合法
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //对分页参数进行校正,就是如果前端传过来的参数是错误的哪怕是正确的也要用方法进行对应的处理按照指定的规则设置
        dto.checkParam();

        //设置查询条件
        //创建条件构造器
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (dto.getIsCollection() != null && dto.getIsCollection() == 1) {
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection,1);
        }
        //添加用户id
        WmUser wmUser = WmThreadLocalUtil.getUser();
        lambdaQueryWrapper.eq(WmMaterial::getUserId,wmUser.getId());

        //列表按照时间进行排序
        lambdaQueryWrapper.orderByAsc(WmMaterial::getCreatedTime);

        //查询
        IPage<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
         page = page(page, lambdaQueryWrapper);
         //封装返回的结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;

    }
}
